package jp.bsws.action.syukei;

/*
 * ログインサンプル
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.KeyValueObjectBean;
import jp.bsws.bean.MasterBean;
import jp.bsws.bean.RaitenJohoBean;
import jp.bsws.bl.BSWSBl;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.HosyuShubetsuDao;
import jp.bsws.dao.JugyoinDao;
import jp.bsws.dao.JugyoinRelationDao;
import jp.bsws.dao.RaitenYoinDao;
import jp.bsws.dao.ShiharaiHohoDao;
import jp.bsws.dao.UriageDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.HosyuShubetsuModel;
import jp.bsws.model.RaitenYoinModel;
import jp.bsws.model.ShiharaiHohoModel;
import jp.bsws.model.UriageShubetsuModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/*
 * メインメニューアクションクラス
 */
public class  RaitenJohoAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;
	private String strSansyoYmd;
	private String strSansyoTenpoCod;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	// ログインユーザ
	private List<KeyValueBean> kvTenpoList;
	private List<KeyValueObjectBean> kvoJugyoinHosyuList;
	private List<RaitenJohoBean> raitenJohoList;
	private List<MasterBean> raitenYoinHList;
	private List<MasterBean> uriageShubetsuHList;
	private List<MasterBean> shiharaiHohoHList;
	private List<MasterBean> hosyuShubetsuHList;
	private List<RaitenJohoBean> raitenJohoGokeiList;
	private List<MasterBean> genkinShutsunoList;

	// ■ログインチェック
	public String execute() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}catch(SQLException se){
			throw se;
		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public String preDD() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{
			//日付を1日前に
			int year =  Integer.parseInt(strSansyoYmd.substring(0,4));
			int month =  Integer.parseInt(strSansyoYmd.substring(4,6));
			int date =  Integer.parseInt(strSansyoYmd.substring(6,8));
			Calendar cal = Calendar.getInstance();
			cal.set(year, month - 1, date);
			cal.add(Calendar.DATE,-1);
			Date currentTime = cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd");
			strSansyoYmd = sdf.format(currentTime);

			//画面表示内容の設定
			hyojiJunbi(con);

		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public String nextDD() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{
			//日付を1日後に
			int year =  Integer.parseInt(strSansyoYmd.substring(0,4));
			int month =  Integer.parseInt(strSansyoYmd.substring(4,6));
			int date =  Integer.parseInt(strSansyoYmd.substring(6,8));
			Calendar cal = Calendar.getInstance();
			cal.set(year, month - 1, date);
			cal.add(Calendar.DATE,1);
			Date currentTime = cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd");
			strSansyoYmd = sdf.format(currentTime);

			//画面表示内容の設定
			hyojiJunbi(con);

		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public String updateRaitenJoho() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();

		try{

			BSWSBl bSWSBl = new BSWSBl();
			bSWSBl.updateRaitenJoho(con, bSWSSession, raitenJohoList, strSansyoTenpoCod, strSansyoYmd);

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String updateGenkinJoho() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();

		try{

			BSWSBl bSWSBl = new BSWSBl();
			bSWSBl.updateGenkinJoho(con, bSWSSession, genkinShutsunoList, strSansyoTenpoCod, strSansyoYmd);

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws SQLException,Exception{

		JugyoinRelationDao jugyoinRelationDao = JugyoinRelationDao.getInstance();

		//参照可能店舗リスト
		this.kvTenpoList = bSWSSession.getSansyoTenpoList();

		//参照店舗がnullの場合、セッションから店舗を取得
		if(BSWSUtils.isBlankNull(strSansyoTenpoCod)){
			strSansyoTenpoCod = bSWSSession.getStrTenpoCod();
		}else{
			bSWSSession.setStrTenpoCod(strSansyoTenpoCod) ;
		}

		this.kvoJugyoinHosyuList = jugyoinRelationDao.getJugyoinHosyuList(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod );

		//日付を取得
		if(BSWSUtils.isBlankNull(strSansyoYmd)){
			strSansyoYmd = BSWSUtils.getSysDateYmd();
		}

		//ヘッダー情報取得
		RaitenYoinDao raitenYoinDao = RaitenYoinDao.getInstance();
		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
		ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();
		HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();

		//来店要因情報取得
		List<RaitenYoinModel> raitenYoinList= raitenYoinDao.getRaitenYoinJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
		//売上種別情報取得
		List<UriageShubetsuModel> uriageShubetsuList= uriageShubetsuDao.getUriageShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
		//支払方法情報取得
		List<ShiharaiHohoModel> shiharaiHohoList= shiharaiHohoDao.getShiharaiHohoJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
		//報酬種別情報取得
		List<HosyuShubetsuModel> HosyuShubetsuList= hosyuShubetsuDao.getHosyuShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);

		raitenYoinHList = new ArrayList<MasterBean>();
		uriageShubetsuHList = new ArrayList<MasterBean>();
		shiharaiHohoHList = new ArrayList<MasterBean>();
		hosyuShubetsuHList = new ArrayList<MasterBean>();
		// ヘッダーの取得
		for(RaitenYoinModel raitenYoinModel : raitenYoinList) {
			if(BSWSConstants.C_HYOJI.equals(raitenYoinModel.getStrRaitenYoinHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(raitenYoinModel.getSeq());
				masterBean.setStrValue(raitenYoinModel.getStrRaitenYoinRyakuKnj());
				raitenYoinHList.add(masterBean);
			}
		}
		for(UriageShubetsuModel uriageShubetsuModel : uriageShubetsuList) {
			if(BSWSConstants.C_HYOJI.equals(uriageShubetsuModel.getStrUriageShubetsuHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(uriageShubetsuModel.getSeq());
				masterBean.setStrValue(uriageShubetsuModel.getStrUriageShubetsuRyakuKnj());
				uriageShubetsuHList.add(masterBean);
			}
		}
		for(ShiharaiHohoModel shiharaiHohoModel : shiharaiHohoList) {
			if(BSWSConstants.C_HYOJI.equals(shiharaiHohoModel.getStrShiharaiHohoHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(shiharaiHohoModel.getSeq());
				masterBean.setStrValue(shiharaiHohoModel.getStrShiharaiHohoRyakuKnj());
				masterBean.setHontaiKeijoFlg(shiharaiHohoModel.getStrShiharaiHohoHontaiKeijoFlg());
				shiharaiHohoHList.add(masterBean);
			}
		}
		for(HosyuShubetsuModel hosyuShubetsuModel : HosyuShubetsuList) {
			if(BSWSConstants.C_HYOJI.equals(hosyuShubetsuModel.getStrHosyuShubetsuHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(hosyuShubetsuModel.getSeq());
				masterBean.setStrValue(hosyuShubetsuModel.getStrHosyuShubetsuRyakuKnj());
				masterBean.setStrKeisanMotoCod(hosyuShubetsuModel.getStrKeisanMotoCod());
				masterBean.setBdWariai(hosyuShubetsuModel.getBdWariaiNum());
				hosyuShubetsuHList.add(masterBean);
			}
		}

		BSWSBl bSWSBl = new BSWSBl();
		//来店情報取得
		raitenJohoList = bSWSBl.getRaitenJoho(con, bSWSSession,
				raitenYoinHList,
				uriageShubetsuHList,
				shiharaiHohoHList,
				hosyuShubetsuHList,
				strSansyoTenpoCod, strSansyoYmd);

		RaitenJohoBean tmpGokei = bSWSBl.getGokeiRaitenJohoBean(raitenJohoList);
		raitenJohoGokeiList = new ArrayList<RaitenJohoBean>();
		//合計値取得
		raitenJohoGokeiList.add(tmpGokei);

		UriageDao uriageDao = UriageDao.getInstance();

		// 現金出納情報取得
		genkinShutsunoList = uriageDao.getUriageGenkinShutsunoJoho(con,
				bSWSSession.getStrGroupCod(), strSansyoTenpoCod,
				null,
				Integer.parseInt(strSansyoYmd));
	}

	public String getStrSansyoYmd() {
		return strSansyoYmd;
	}

	public void setStrSansyoYmd(String strSansyoYmd) {
		this.strSansyoYmd = strSansyoYmd;
	}

	public List<KeyValueBean> getKvTenpoList() {
		return kvTenpoList;
	}

	public void setKvTenpoList(List<KeyValueBean> kvTenpoList) {
		this.kvTenpoList = kvTenpoList;
	}

	public List<RaitenJohoBean> getRaitenJohoList() {
		return raitenJohoList;
	}

	public void setRaitenJohoList(List<RaitenJohoBean> raitenJohoList) {
		this.raitenJohoList = raitenJohoList;
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public String getStrSansyoTenpoCod() {
		return strSansyoTenpoCod;
	}

	public void setStrSansyoTenpoCod(String strSansyoTenpoCod) {
		this.strSansyoTenpoCod = strSansyoTenpoCod;
	}

	public List<MasterBean> getRaitenYoinHList() {
		return raitenYoinHList;
	}

	public void setRaitenYoinHList(List<MasterBean> raitenYoinHList) {
		this.raitenYoinHList = raitenYoinHList;
	}

	public List<MasterBean> getUriageShubetsuHList() {
		return uriageShubetsuHList;
	}

	public void setUriageShubetsuHList(List<MasterBean> uriageShubetsuHList) {
		this.uriageShubetsuHList = uriageShubetsuHList;
	}

	public List<MasterBean> getShiharaiHohoHList() {
		return shiharaiHohoHList;
	}

	public void setShiharaiHohoHList(List<MasterBean> shiharaiHohoHList) {
		this.shiharaiHohoHList = shiharaiHohoHList;
	}

	public List<MasterBean> getHosyuShubetsuHList() {
		return hosyuShubetsuHList;
	}

	public void setHosyuShubetsuHList(List<MasterBean> hosyuShubetsuHList) {
		this.hosyuShubetsuHList = hosyuShubetsuHList;
	}

	public List<RaitenJohoBean> getRaitenJohoGokeiList() {
		return raitenJohoGokeiList;
	}

	public void setRaitenJohoGokeiList(List<RaitenJohoBean> raitenJohoGokeiList) {
		this.raitenJohoGokeiList = raitenJohoGokeiList;
	}

	public List<MasterBean> getGenkinShutsunoList() {
		return genkinShutsunoList;
	}

	public void setGenkinShutsunoList(List<MasterBean> genkinShutsunoList) {
		this.genkinShutsunoList = genkinShutsunoList;
	}

	public List<KeyValueObjectBean> getKvoJugyoinHosyuList() {
		return kvoJugyoinHosyuList;
	}

	public void setKvoJugyoinHosyuList(List<KeyValueObjectBean> kvoJugyoinHosyuList) {
		this.kvoJugyoinHosyuList = kvoJugyoinHosyuList;
	}

}